package com.rp.demo;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.assertj.core.error.ShouldBeToday;

import java.io.FileOutputStream;

/**
 * 导出带样式的excel
 * 高版本
 * 1.合并单元格 2.加边框 3.行高列宽 4 对齐方式 5.字体
 */
public class POIStyleDemo03 {

    public static void main(String[] args) throws Exception {
        //1.创建一个全新的 里面什么都没有的 工作簿
        Workbook workbook = new XSSFWorkbook();



        //2 创建一个全新的工作表
        Sheet sheet = workbook.createSheet("带有样式高版本poi");
        //设置表格宽度
        sheet.setColumnWidth(0,5*256);
        sheet.setColumnWidth(1,10*256);
        sheet.setColumnWidth(2,15*256);
        sheet.setColumnWidth(3,15*256);
        sheet.setColumnWidth(4,30*256);



        //1.1 设置单元格样式:
        //需求1.边框线:全边框  2.行高:42  3.合并单元格 : 第一行的第一个单元格到第五个单元格
        //对齐方式: 水平垂直都要居中
        //字体:黑体18号
        CellStyle bigTitleStyle = workbook.createCellStyle();
        //设置边框
        bigTitleStyle.setBorderBottom(BorderStyle.THIN);
        bigTitleStyle.setBorderTop(BorderStyle.THIN);
        bigTitleStyle.setBorderLeft(BorderStyle.THIN);
        bigTitleStyle.setBorderRight(BorderStyle.THIN);

        //设置对齐放回寺
        bigTitleStyle.setAlignment(HorizontalAlignment.CENTER);//水平
        bigTitleStyle.setVerticalAlignment(VerticalAlignment.CENTER);//居中

        //设置字体颜色
        Font font = workbook.createFont();
        font.setFontName("黑体");
        font.setFontHeightInPoints((short)18);
        bigTitleStyle.setFont(font);

        //3 创建了第一行
        Row bigtitleRow = sheet.createRow(0);
        //设置行高
        bigtitleRow.setHeightInPoints(42);

        for (int i = 0; i < 5; i++) {
            Cell cell = bigtitleRow.createCell(i);
            cell.setCellStyle(bigTitleStyle);
        }

        //合并单元格
        sheet.addMergedRegion(new CellRangeAddress(0,0,0,4)); //int firstRow, int lastRow, int firstCol, int lastCol


        //向单元格中设置值
        sheet.getRow(0).getCell(0).setCellValue("创建一个工作簿 里面随便写句话");

       //=========================设置大标题结束  ,  设置小标题开始================================================


        CellStyle litleCellStyle = workbook.createCellStyle();
        litleCellStyle.cloneStyleFrom(bigTitleStyle);
        //设置字体颜色
        Font litlefont = workbook.createFont();
        litlefont.setFontName("宋体");
        litlefont.setFontHeightInPoints((short)12);
        litlefont.setBold(true);//加粗
        litleCellStyle.setFont(litlefont);


        Row titleRow = sheet.createRow(1);
        titleRow.setHeightInPoints(30);
        String[] titles = new String[]{"编号","姓名","手机号","入职日期","现住址"};

        for (int i = 0; i < titles.length; i++) {
            Cell cell = titleRow.createCell(i);
            cell.setCellStyle(litleCellStyle);
            cell.setCellValue(titles[i]);
        }
        //=========================设置小标题结束  ,  设置内容开始================================================


        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.cloneStyleFrom(bigTitleStyle);
        cellStyle.setAlignment(HorizontalAlignment.LEFT);//向左靠齐
        //设置字体颜色
        Font contentFont = workbook.createFont();
        contentFont.setFontName("宋体");
        contentFont.setFontHeightInPoints((short)11);
        contentFont.setBold(false);//加粗
        cellStyle.setFont(litlefont);


        Row row = sheet.createRow(2);
        String[] titles1 = new String[]{"编号","姓名","手机号","入职日期","现住址"};

        for (int i = 0; i < titles1.length; i++) {
            Cell cell = row.createCell(i);
            cell.setCellStyle(cellStyle);
            cell.setCellValue(titles1[i]);
        }



        //6 将生产的excel输出到磁盘
        workbook.write(new FileOutputStream("/Users/rp/Downloads/test/test02.xlsx"));
        workbook.close();
    }
}
